﻿using System.Collections.Generic;

namespace BestEasyCMS.Framework.Querying
{
    /// <summary>
    /// 查询对象
    /// </summary>
    public class Query
    {
        private readonly IList<Query> _subQueries = new List<Query>();
        private readonly IList<Criterion> _criteria;//= new List<Criterion>();
        private readonly QueryName _name;
        public string ProcedureName { get; private set; }

        public Query()
            : this(QueryName.Dynamic, new List<Criterion>())
        {

        }
        public Query(QueryName name, IList<Criterion> criteria)
        {
            _name = name;
            _criteria = criteria;
        }
        public Query(string procedureName, IList<Criterion> criteria)
        {
            _name = QueryName.Procedure;
            ProcedureName = procedureName;
            _criteria = criteria;
        }

        public QueryName Name { get { return _name; } }

        public bool IsNamedQuery()
        {
            return Name != QueryName.Dynamic;
        }

        /// <summary>
        /// 标准查询列表
        /// </summary>
        public IEnumerable<Criterion> Criteria
        {
            get { return _criteria; }
        }
        /// <summary>
        /// 子查询
        /// </summary>
        public IEnumerable<Query> SubQueries
        {
            get { return _subQueries; }
        }
        /// <summary>
        /// 添加子查询
        /// </summary>
        /// <param name="subQuery"></param>
        public void AddSubQuery(Query subQuery)
        {
            _subQueries.Add(subQuery);
        }
        /// <summary>
        /// 添加标准查询
        /// </summary>
        /// <param name="criterion"></param>
        public void Add(Criterion criterion)
        {
            _criteria.Add(criterion);
        }
        /// <summary>
        /// 查询操作
        /// </summary>
        public QueryOperator QueryOperator { get; set; }
        /// <summary>
        /// 排序方式
        /// </summary>
        public OrderByClause OrderByProperty { get; set; }
    }

}
